package st.business.tepth.business.user.operationchoose;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.DiagnoseBean;
import st.business.tepth.business.bean.OperationBean;
import st.business.tepth.business.bean.ProgramBean;
import st.business.tepth.business.event.AddProgramEvent;
import st.business.tepth.config.ItemFields;
import st.business.tepth.manager.GlobalDataManager;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.recycler.MultipleItemEntity;

import static st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter.TYPE_MANUAL_UN_CONFIRM;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-19 09:40
 */

public class OperationChooseDelegate extends MvpFragment<OperationChoosePresenter> implements OperationChooseContract.View {
    public static OperationChooseDelegate newInstance(DiagnoseBean diagnoseBean, int repairId) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("diagnoseBean", diagnoseBean);
        bundle.putInt("repairId", repairId);
        OperationChooseDelegate delegate = new OperationChooseDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private OperationAdapter mOperationAdapter;
    private DialogLoader mDialogLoader;
    private DiagnoseBean mDiagnoseBean;
    private int mRepairId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mDiagnoseBean = bundle.getParcelable("diagnoseBean");
            mRepairId = bundle.getInt("repairId");
        }
    }

    @Override
    public OperationChoosePresenter getPresenter() {
        return new OperationChoosePresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_operation_choose;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText("选择动作");
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity, 6));
        List<OperationBean> operationBeans = GlobalDataManager.getInstance().getOperationBeans();
        mOperationAdapter = new OperationAdapter(R.layout.item_operation, operationBeans,
                new OnItemChooseListener<OperationBean>() {
                    @Override
                    public void onChoose(OperationBean operationBean) {
                        ProgramBean programBean = new ProgramBean();
                        if (mDiagnoseBean != null) {
                            programBean.setDiagnostic_name(mDiagnoseBean.getName());
                            programBean.setPhenomenon_id(mDiagnoseBean.getRepair_phenomenon_id());
                            programBean.setFault_diagnostic_id(mDiagnoseBean.getId());
                        }
                        programBean.setAction_name(operationBean.getAction_name());
                        programBean.setMaintenance_action_id(operationBean.getId());
                        MultipleItemEntity entity = MultipleItemEntity.builder()
                                .setItemType(TYPE_MANUAL_UN_CONFIRM)
                                .setField(ItemFields.ITEM_OBJ, programBean)
                                .build();
                        EventBus.getDefault().post(new AddProgramEvent(mRepairId, entity));
                        pop();
                    }
                });
        mRecyclerView.setAdapter(mOperationAdapter);
        mOperationAdapter.setEmptyView(R.layout.view_search_empty, (ViewGroup) mRecyclerView.getParent());
        if (operationBeans == null || operationBeans.isEmpty()) {
            mDialogLoader = new DialogLoader(_mActivity);
            mDialogLoader.showProgressDialog(false, "正在请求", "请稍候...");
            mPresenter.getOperationList(UserManager.getInstance().getToken());
        }

    }

    @Override
    public void showOperationList(List<OperationBean> list) {
        if (mOperationAdapter != null && list != null && !list.isEmpty()) {
            mOperationAdapter.setNewData(list);
            GlobalDataManager.getInstance().setOperationBeans(list);
        }
        if (mDialogLoader != null) {
            mDialogLoader.clearDialog();
        }
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @Override
    public void showError(int code, String msg) {
        if (mDialogLoader != null) {
            mDialogLoader.stopProgressWithFailed(msg, msg);
        }
    }

    private class OperationAdapter extends BaseQuickAdapter<OperationBean, BaseViewHolder> {
        private int mColorN = Color.parseColor("#8fa3b7");
        private int mColorP = Color.parseColor("#f1b647");
        private int mOldPosition = -1;
        private OnItemChooseListener<OperationBean> mChooseListener;

        public OperationAdapter(int layoutResId,
                                @Nullable List<OperationBean> data,
                                OnItemChooseListener<OperationBean> listener) {
            super(layoutResId, data);
            mChooseListener = listener;
        }

        @Override
        protected void convert(final BaseViewHolder helper, final OperationBean item) {
            helper.setText(R.id.tv_bus_number, item.getAction_name());
            if (item.isSelected()) {
                helper.setTextColor(R.id.tv_bus_number, mColorP);
                helper.setBackgroundRes(R.id.tv_bus_number, R.drawable.shape_item_operation_c);
            } else {
                helper.setTextColor(R.id.tv_bus_number, mColorN);
                helper.setBackgroundRes(R.id.tv_bus_number, R.drawable.shape_item_operation_n);
            }
            helper.getView(R.id.tv_bus_number).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = helper.getAdapterPosition();
                    if (position != mOldPosition) {
                        item.setSelected(true);
                        helper.setTextColor(R.id.tv_bus_number, mColorP);
                        helper.setBackgroundRes(R.id.tv_bus_number, R.drawable.shape_item_operation_c);
                        if (mOldPosition != -1) {
                            OperationBean old = getItem(mOldPosition);
                            if (old != null) {
                                old.setSelected(false);
                                notifyItemChanged(mOldPosition);
                            }
                        }
                        mOldPosition = position;
                    }
                    if (mChooseListener != null) {
                        mChooseListener.onChoose(item);
                    }
                }
            });
        }
    }
}